package com.bxd.baodan.fragment;

import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;
import com.bxd.baodan.App;
import com.bxd.baodan.R;
import com.bxd.baodan.activity.AnswerDetailActivity;
import com.bxd.baodan.activity.QuestionDetailActivity;
import com.bxd.baodan.adapter.ViewHolder;
import com.bxd.baodan.app.JsonApi;
import com.bxd.baodan.base.BaseListFragment_Sticky;
import com.bxd.baodan.item.QuestionItem;
import com.bxd.baodan.util.GlideUtil;
import com.bxd.baodan.util.NetUtil;
import com.umeng.analytics.MobclickAgent;

import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.List;

/**
 * Created by Administrator on 2017/11/6.
 */

public class QuestionListFragment extends BaseListFragment_Sticky<QuestionItem> {

    public String id;//防止空指针

    public static QuestionListFragment newInstance(String id) {
        QuestionListFragment fragment = new QuestionListFragment();
        fragment.id = id;

        return fragment;
    }

    @Override
    protected void preDone(View v) {
        listView.setDividerHeight((int) getActivity().getResources().getDimension(R.dimen.devider));
//        refView.setMode(PullToRefreshBase.Mode.PULL_FROM_END);
//        refView.setRefreshing();
        autoRefresh();
    }

    @Override
    protected int getFragmentLayout() {
        return R.layout.pull_refresh_layout_sticky;
    }

    @Override
    protected Type getTypeToken() {
        return new TypeToken<List<QuestionItem>>() {
        }.getType();
    }

    @Override
    protected void setUsualNetUtil() {
        if (id.equals("my")) {
            netUtil = new NetUtil(getActivity(), JsonApi.QUESTION_MYQUESTION);
        } else {
            netUtil = new NetUtil(getActivity(), JsonApi.QUESTION);
            netUtil.setParams("tagId", id);
        }

    }

    @Override
    protected int getItemLayout() {
        return R.layout.question_detail_item;
    }

    @Override
    protected void convert(ViewHolder holder, final QuestionItem item) {

        // 问题
        TextView questioner = (TextView) holder.getView(R.id.asker);
        ImageView quesAvatar = (ImageView) holder.getView(R.id.asker_img);
        TextView quesTime = (TextView) holder.getView(R.id.ask_time);
        TextView quesContent = (TextView) holder.getView(R.id.question_content);
        TextView replayCount = (TextView) holder.getView(R.id.answer_count);


        RelativeLayout answerLayout = (RelativeLayout) holder.getView(R.id.answer_layout);
        ImageView answerAvatar = (ImageView) holder.getView(R.id.answer_avatar);
        TextView answer = (TextView) holder.getView(R.id.answer_name);
        TextView answerContent = (TextView) holder.getView(R.id.answer_content);
        ImageView verify_icon = (ImageView) holder.getView(R.id.verify_icon);
        ImageView official_iv = (ImageView) holder.getView(R.id.official_iv);
        ImageView robot_sign = (ImageView) holder.getView(R.id.robot_sign);
        ImageView is_bestAnswer = (ImageView) holder.getView(R.id.iv_is_bestAnswer);

        GlideUtil.loadCircle(getActivity(), item.getU_avatar(), quesAvatar);
        questioner.setText(item.getU_nick());
        quesTime.setText(item.getQ_create_date());
        quesContent.setText(item.getQ_title());
        replayCount.setText(item.getQ_answer_count() + "");

        holder.getView(R.id.main).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                  MobclickAgent.onEvent(App.getContext(), "v6_1_3_BW_wdxq");
                openActivity(QuestionDetailActivity.class, new String[]{"qid", "q_title"}, new String[]{item.getQ_id() + "", item.getQ_title()});
            }
        });

        if (item.getQa_answer_info() != null && item.getQa_answer_info().getQa_id() != null) {

            answerLayout.setVisibility(View.VISIBLE);

            answerLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    openActivity(AnswerDetailActivity.class,new String[]{"qid","qaid"},new String[]{item.getQ_id()+"",item.getQa_answer_info().getQa_id()});

                }
            });
            if (item.getQa_answer_info().getIsBestAnswer() == 1) {
                answerLayout.setBackground(getResources().getDrawable(R.drawable.shape_yellow_a7_r));
                is_bestAnswer.setVisibility(View.VISIBLE);

            } else {
                answerLayout.setBackground(getResources().getDrawable(R.drawable.shape_gray_a7_r));
                is_bestAnswer.setVisibility(View.GONE);
            }


            GlideUtil.loadCircle(getActivity(), item.getQa_answer_info().getAvatar(), answerAvatar);
            answer.setText(item.getQa_answer_info().getName());
            answerContent.setText(item.getQa_answer_info().getQa_title());

            // TODO: 2017/3/27 身份认证要改
            if (item.getQa_answer_info().agent_mark != null && item.getQa_answer_info().agent_mark.length > 0
                    && Arrays.asList(item.getQa_answer_info().agent_mark).contains("6")) {
                verify_icon.setVisibility(View.VISIBLE);
            } else {
                verify_icon.setVisibility(View.GONE);
            }
            if (item.getQa_answer_info().agent_mark != null && item.getQa_answer_info().agent_mark.length > 0
                    && Arrays.asList(item.getQa_answer_info().agent_mark).contains("8")) {
                official_iv.setVisibility(View.VISIBLE);
            } else {
                official_iv.setVisibility(View.GONE);
            }
            if (item.getQa_answer_info().src != null && item.getQa_answer_info().src.equals("6")) {
                robot_sign.setVisibility(View.VISIBLE);
            } else {
                robot_sign.setVisibility(View.GONE);
            }



        } else {
            answerLayout.setVisibility(View.GONE);
        }
    }

    @Override
    protected String getArrayStr(String json_data) {
        return "list";
    }
}
